﻿using System;
using System.Collections.Generic;
using System.Text;
using XiaoGang.Soft.XGWMS.DAL_SQL;
using XiaoGang.Soft.XGWMS.COM;
using XiaoGang.Soft.XGWMS.MODEL;
using System.Transactions;

namespace XiaoGang.Soft.XGWMS.BLL
{
    public class BLL_M002_USER_GROUP
    {
        private readonly DAL_M002_USER_GROUP dalUserGroup = new DAL_M002_USER_GROUP();

        /// <summary>
        /// AddUserGroup
        /// </summary>
        /// <param name="modUserGroup"></param>
        /// <returns></returns>
        public ReturnValue AddOrUpdateUserGroup(MOD_M002_USER_GROUP modUserGroup)
        {
            ReturnValue returnValue = new ReturnValue(false, "E12001");//添加用户分组失败

            if (modUserGroup.GROUP_ID != "")
            {
                //更新
                //首先检查不同ID但同名称的用户分组
                returnValue = dalUserGroup.CheckUserGroupNameWithoutID(modUserGroup.GROUP_ID, modUserGroup.GROUP_NM);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                if (returnValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    //return new ReturnValue(false, "W11018");//该用户组名称已经存在，或已被逻辑删除。
                    return new ReturnValue(false, "W11H01");//本[@]已经存在或已被逻辑删除!
                }
                //检查是否存在该ID的数据
                returnValue = dalUserGroup.GetUserGroup(modUserGroup.GROUP_ID);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                if (returnValue.ResultDataSet.Tables[0].Rows.Count < 1)
                {
                    return new ReturnValue(false, "W11013");//用户组不存在！
                }
                //进行更新
                return dalUserGroup.UpdateUserGroup(modUserGroup);
            }
            else
            {
                //新添加
                //首先检查名称是否已经存在
                returnValue = dalUserGroup.CheckUserGroupName(modUserGroup.GROUP_NM);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                if (returnValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    //return new ReturnValue(false, "W11018");//该用户组名称已经存在
                    return new ReturnValue(false, "W11H01");//本[@]已经存在或已被逻辑删除!
                }
                //生成编号
                //BLL_ComProcess bllCompress = new BLL_ComProcess();
                //MOD_M011_GET_ID modGetID = new MOD_M011_GET_ID();
                //modGetID.UPDATER_ID = modUserGroup.UPDATER_ID;
                //modGetID.TERMINAL_CD = modUserGroup.TERMINAL_CD;
                //modGetID.GETID_DIV = "00";
                //modGetID.FISCAL_YEAR = System.DateTime.Now.Year.ToString("####");
                //returnValue = bllCompress.GetSlipID(modGetID);
                //if (!returnValue.ErrorFlag)
                //{
                //    return returnValue;
                //}
                //modUserGroup.GROUP_ID = returnValue.ResultDataSet.Tables[0].Rows[0]["SLIPID"].ToString();
                string UniqueNo = string.Empty;
                if (!DAL_ComProcess.GetUniqueNo(modUserGroup.UPDATER_ID
                    , modUserGroup.TERMINAL_CD
                    , "00"
                    , System.DateTime.Now.Year.ToString("####")
                    , out UniqueNo))
                {
                    return new ReturnValue(false, "E11003");//生成业务编号失败
                }
                modUserGroup.GROUP_ID = UniqueNo;
                //进行添加
                returnValue = dalUserGroup.AddUserGroup(modUserGroup);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                returnValue.ErrorID = modUserGroup.GROUP_ID;
                return returnValue;
            }
        }

        /// <summary>
        /// GerUserGroupInfo
        /// </summary>
        /// <param name="modUserGroup"></param>
        /// <returns></returns>
        public ReturnValue GerUserGroupInfo(MOD_M002_USER_GROUP modUserGroup)
        {
            return dalUserGroup.GetUserGroup(modUserGroup.GROUP_ID, modUserGroup.GROUP_NM, modUserGroup.MEMO);
        }

        /// <summary>
        /// UpdateUserGroup
        /// </summary>
        /// <param name="modUserGroup"></param>
        /// <returns></returns>
        public ReturnValue UpdateUserGroup(MOD_M002_USER_GROUP modUserGroup)
        {
            ReturnValue returnValue = dalUserGroup.CheckUserGroupNameWithoutID(modUserGroup.GROUP_ID, modUserGroup.GROUP_NM);
            if (!returnValue.IsSuccess)
            {
                return returnValue;
            }
            if (returnValue.ResultDataSet.Tables[0].Rows.Count > 0)
            {
                //return new ReturnValue(false, "W11018");//This UserGroupName Has Exist!
                return new ReturnValue(false, "W11H01");//本[@]已经存在或已被逻辑删除!
            }
            returnValue = dalUserGroup.GetUserGroup(modUserGroup.GROUP_ID, "", "");
            if (!returnValue.IsSuccess)
            {
                return returnValue;
            }
            if (returnValue.ResultDataSet.Tables[0].Rows.Count < 1)
            {
                return new ReturnValue(false, "W11013");//The UserGroup is not existed！
            }
            return dalUserGroup.UpdateUserGroup(modUserGroup);
        }

        /// <summary>
        /// 物理删除用户分组
        /// </summary>
        /// <param name="modUserGroup"></param>
        /// <returns></returns>
        public ReturnValue PhysicalDelUserGroup(string UserGroupID)
        {
            ReturnValue returnValue = dalUserGroup.GetUserGroup(UserGroupID);
            if (!returnValue.IsSuccess)
            {
                return returnValue;
            }
            if (returnValue.ResultDataSet.Tables[0].Rows.Count < 1)
            {
                return new ReturnValue(false, "W11013");
            }
            return dalUserGroup.DeleteUserGroup(UserGroupID);
        }

        /// <summary>
        /// 检查用户分组是否被使用
        /// </summary>
        /// <param name="UserGroupID"></param>
        /// <returns></returns>
        public ReturnValue GetUserGroupUsed(string UserGroupID)
        {
            return dalUserGroup.GetUserGroupUsed(UserGroupID);
        }
    }
}
